unit u_Contratos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai, DB, Menus, XPMenu, StdCtrls, Buttons, ExtCtrls, MMJPanel,
  Grids, DBGrids, JvExDBGrids, JvDBGrid, JvExMask, JvToolEdit,
  JvDBControls, Mask, DBCtrls, FMTBcd, DBClient, Provider, SqlExpr,
  DBLocal, DBLocalS, ACBrUtil, JvBaseEdits;

type
  Tf_Contratos = class(TfPai)
    MMJPanel3: TMMJPanel;
    lbl10: TLabel;
    dbedtCTR_N_CONTRATO: TDBEdit;
    dbedtNOMEUSUARIO: TDBEdit;
    lbl11: TLabel;
    btn1: TBitBtn;
    btn2: TBitBtn;
    dbedtNOMEFORNECEDOR: TDBEdit;
    lbl12: TLabel;
    JvDBDateEdit1: TJvDBDateEdit;
    lbl1: TLabel;
    lbl2: TLabel;
    JvDBDateEdit2: TJvDBDateEdit;
    dbedtCTR_SITUACAO: TDBEdit;
    lbl3: TLabel;
    dbedtCTR_QUANTIDADETOTAL: TDBEdit;
    lbl4: TLabel;
    lbl5: TLabel;
    dbedtCTR_QUTDECONTRATADA: TDBEdit;
    lbl6: TLabel;
    dbedtCTR_QUTDEENTREGUE: TDBEdit;
    dbedtCTR_OBS: TDBEdit;
    lbl8: TLabel;
    JvDBGrid2: TJvDBGrid;
    s_Contrato: TSQLDataSet;
    p_Contrato: TDataSetProvider;
    ds_contrato: TClientDataSet;
    ds2: TDataSource;
    ds3: TClientDataSet;
    DataSetProvider2: TDataSetProvider;
    SQLDataSet2: TSQLDataSet;
    btn3: TBitBtn;
    dbedtNOMEUSUARIO1: TDBEdit;
    lbl7: TLabel;
    s_ContratoCTR_ID: TIntegerField;
    s_ContratoFOR_ID: TIntegerField;
    s_ContratoPRO_ID: TIntegerField;
    s_ContratoCTR_N_CONTRATO: TStringField;
    s_ContratoCTR_DATAINICONTRATO: TDateField;
    s_ContratoCTR_DATAFIMCONTRATO: TDateField;
    s_ContratoCTR_OBS: TStringField;
    s_ContratoCTR_QUTDENF: TIntegerField;
    s_ContratoCTR_QUANTIDADETOTAL: TFloatField;
    s_ContratoCTR_QUTDECONTRATADA: TFloatField;
    s_ContratoCTR_QUTDEENTREGUE: TFloatField;
    s_ContratoCTR_TURMEIRO: TIntegerField;
    s_ContratoCTR_COMPRADOR: TIntegerField;
    s_ContratoCTR_SALDO: TFloatField;
    s_ContratoCTR_SITUACAO: TStringField;
    s_ContratoEMPREITEIRO: TStringField;
    s_ContratoPRO_DESCRICAO: TStringField;
    ds_contratoCTR_ID: TIntegerField;
    ds_contratoFOR_ID: TIntegerField;
    ds_contratoPRO_ID: TIntegerField;
    ds_contratoCTR_N_CONTRATO: TStringField;
    ds_contratoCTR_DATAINICONTRATO: TDateField;
    ds_contratoCTR_DATAFIMCONTRATO: TDateField;
    ds_contratoCTR_OBS: TStringField;
    ds_contratoCTR_QUTDENF: TIntegerField;
    ds_contratoCTR_QUANTIDADETOTAL: TFloatField;
    ds_contratoCTR_QUTDECONTRATADA: TFloatField;
    ds_contratoCTR_QUTDEENTREGUE: TFloatField;
    ds_contratoCTR_TURMEIRO: TIntegerField;
    ds_contratoCTR_COMPRADOR: TIntegerField;
    ds_contratoCTR_SALDO: TFloatField;
    ds_contratoCTR_SITUACAO: TStringField;
    ds_contratoEMPREITEIRO: TStringField;
    ds_contratoPRO_DESCRICAO: TStringField;
    busca_propriedade: TSQLClientDataSet;
    busca_propriedadePRO_ID: TIntegerField;
    busca_propriedadePRO_DESCRICAO: TStringField;
    btn8: TBitBtn;
    dbedtPRO_DESCRICAO: TDBEdit;
    lbl9: TLabel;
    btn4: TBitBtn;
    s_ContratoPROPRIETARIO: TStringField;
    ds_contratoPROPRIETARIO: TStringField;
    btn5: TBitBtn;
    busca_propriedadePRO_QUANTIDADETOTAL: TFloatField;
    JvDBCalcEdit1: TJvDBCalcEdit;
    lbl13: TLabel;
    JvDBCalcEdit2: TJvDBCalcEdit;
    lbl14: TLabel;
    lbl15: TLabel;
    JvDBCalcEdit3: TJvDBCalcEdit;
    s_ContratoVALOR_FRETE: TFloatField;
    s_ContratoVALOR_COLHEITA: TFloatField;
    s_ContratoVALOR_COMICAO: TFloatField;
    ds_contratoVALOR_FRETE: TFloatField;
    ds_contratoVALOR_COLHEITA: TFloatField;
    ds_contratoVALOR_COMICAO: TFloatField;
    s_ContratoCOMPRADOR: TStringField;
    ds_contratoCOMPRADOR: TStringField;
    btn6: TBitBtn;
    buscaSafra: TSQLClientDataSet;
    buscaFrete: TSQLClientDataSet;
    buscaSafraDESCSAFRA: TStringField;
    buscaFreteFAIXA: TStringField;
    buscaFreteVALOR_FAIXA: TFloatField;
    dbedtCTR_N_CONTRATO1: TDBEdit;
    lbl16: TLabel;
    btn7: TBitBtn;
    s_ContratoSAFRA: TStringField;
    ds_contratoSAFRA: TStringField;
    dbcbbImpostos: TDBComboBox;
    lbl17: TLabel;
    s_ContratoAPLICAIMPOSTO: TStringField;
    ds_contratoAPLICAIMPOSTO: TStringField;
    scds_forn_proc: TSQLClientDataSet;
    scds_forn_procCODFORNECEDOR: TIntegerField;
    scds_forn_procNOMEFORNECEDOR: TStringField;
    scds_forn_procRAZAOSOCIAL: TStringField;
    scds_forn_procDDD: TSmallintField;
    scds_forn_procTELEFONE: TStringField;
    scds_forn_procPRAZOPAGAMENTO: TSmallintField;
    scds_forn_procCFOP: TStringField;
    procedure btnProcurarClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btn2Click(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
    procedure btn6Click(Sender: TObject);
    procedure btn7Click(Sender: TObject);
    procedure btnIncluirClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  f_Contratos: Tf_Contratos;

implementation

uses UDm, uDmCitrus, uProcurar, u_VariedadeContrato;

{$R *.dfm}

procedure Tf_Contratos.btnProcurarClick(Sender: TObject);
begin
  //  inherited;

end;

procedure Tf_Contratos.FormCreate(Sender: TObject);
begin
//  inherited;

end;

procedure Tf_Contratos.btn1Click(Sender: TObject);
begin
//  inherited;
  if (ds_contrato.State = dsInactive) then
     Exit;

  if (ds_contrato.State = dsBrowse) then
     ds_contrato.Edit;

  fProcurar:= TfProcurar.Create(self,DmCitrus.buscaComprador);
  try
    fProcurar.BtnProcurar.Click;
    fProcurar.EvDBFind1.DataField     := 'NOMEFORNECEDOR';
    fProcurar.RadioGroup2.Visible     := True;
   if fProcurar.ShowModal=mrOk then
    begin
    if dtSrc.State=dsBrowse then
     ds_contrato.Edit;
     ds_contratoCTR_COMPRADOR.AsInteger := DmCitrus.buscaCompradorCODFORNECEDOR.AsInteger;
     ds_contratoCOMPRADOR.AsString    :=  DmCitrus.buscaCompradorNOMEFORNECEDOR.AsString;
    end;
  finally
    DmCitrus.buscaComprador.Close;
    fProcurar.Free;
  end;
{
  fProcurar:= TfProcurar.Create(self,dm.scds_usuario_proc);
  fProcurar.usuarioproc := 'COMPRADOR';
  fProcurar.BtnProcurar.Click;
  fProcurar.EvDBFind1.DataField := 'NOMEUSUARIO';
  try
   if fProcurar.ShowModal=mrOk then
    begin
    if dtSrc.State=dsBrowse then
     ds_contrato.Edit;
     ds_contratoCTR_COMPRADOR.AsInteger := dm.scds_usuario_ProcCODusuario.AsInteger;
     ds_contratoNOMEUSUARIO.AsString :=  dm.scds_usuario_procNOMEUSUARIO.AsString;
    end;
  finally
    dm.scds_usuario_proc.Close;
    fProcurar.Free;
  end;
  }
end;

procedure Tf_Contratos.FormShow(Sender: TObject);
begin
//   inherited;
  if (ds_contrato.Active) then
     ds_contrato.Close;
  ds_contrato.Params[0].AsInteger := DM.ID_FORNECEDOR;
  ds_contrato.Open;
  if (ds_contrato.IsEmpty) then
    ds_contrato.Close;
end;

procedure Tf_Contratos.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if (ds_contrato.Active) then
     ds_contrato.Close;
  inherited;

end;

procedure Tf_Contratos.btn2Click(Sender: TObject);
begin
//  inherited;
  if (ds_contrato.State = dsInactive) then
     Exit;

  if (ds_contrato.State = dsBrowse) then
     ds_contrato.Edit;

  fProcurar:= TfProcurar.Create(self,DmCitrus.buscaColhedor);
  try
    fProcurar.BtnProcurar.Click;
    fProcurar.EvDBFind1.DataField     := 'NOMEFORNECEDOR';
    fProcurar.RadioGroup2.Visible     := True;
    fProcurar.ShowModal;
    ds_contratoCTR_TURMEIRO.AsInteger := DmCitrus.buscaColhedorCODFORNECEDOR.AsInteger;
    ds_contratoEMPREITEIRO.AsString   := DmCitrus.buscaColhedorNOMEFORNECEDOR.AsString;
  finally
    DmCitrus.buscaColhedor.Close;
    fProcurar.Free;
  end;
end;

procedure Tf_Contratos.btnGravarClick(Sender: TObject);
begin
  if (ds_contrato.State in [dsInsert]) then
  begin
    if dm.c_6_genid.Active then
      dm.c_6_genid.Close;
    dm.c_6_genid.CommandText := 'SELECT CAST(GEN_ID(gen_contratos, 1) AS INTEGER) AS CODIGO FROM RDB$DATABASE';
    dm.c_6_genid.Open;
    ds_contratoCTR_ID.AsInteger := dm.c_6_genid.Fields[0].AsInteger;
    ds_contratoFOR_ID.AsInteger := dm.ID_FORNECEDOR;
    dm.c_6_genid.Close;
    ds_contratoCTR_SITUACAO.AsString := 'EM ABERTO';
    ds_contratoCTR_N_CONTRATO.AsString := IntToStr(ds_contratoCTR_ID.AsInteger) + IntToStr(dm.ID_FORNECEDOR) + padR(IntToStr(DM.ID_FORNECEDOR),6,'0');
  end;
  inherited;
 { if (ds_contrato.Active) then
     ds_contrato.Close;
  ds_contrato.Params[0].AsInteger := DM.ID_FORNECEDOR;
  ds_contrato.Open;
  }
end;

procedure Tf_Contratos.btn3Click(Sender: TObject);
begin
  inherited;
    if (ds_contrato.State = dsInactive) then
     Exit;

   if (ds_contrato.State = dsBrowse) then
     ds_contrato.Edit;

  fProcurar:= TfProcurar.Create(self,busca_propriedade);
  fProcurar.btnImprimir.Visible := true;
  fProcurar.RadioGroup1.Visible := true;
  busca_propriedade.Params.ParamByName('id').AsInteger := DM.ID_FORNECEDOR;
  fProcurar.BtnProcurar.Click;
  fProcurar.EvDBFind1.DataField := 'PRO_DESCRICAO';
  fProcurar.RadioGroup2.Visible := True;
  try
    if fProcurar.ShowModal=mrOk then
    begin
      if dtSrc.State=dsBrowse then
       ds_contrato.Edit;
      ds_contratoPRO_ID.AsInteger := busca_propriedadePRO_ID.AsInteger;
      ds_contratoPRO_DESCRICAO.AsString :=  busca_propriedadePRO_DESCRICAO.AsString;
      ds_contratoCTR_QUANTIDADETOTAL.Value :=  busca_propriedadePRO_QUANTIDADETOTAL.Value;
    end;
  finally
    busca_propriedade.Close;
    fProcurar.Free;
  end;
end;

procedure Tf_Contratos.btn4Click(Sender: TObject);
begin
//  inherited;
  if (ds_contrato.State = dsInactive) then
     Exit;

  if (ds_contrato.State = dsBrowse) then
     ds_contrato.Edit;
     
  fProcurar:= TfProcurar.Create(self,scds_forn_proc);
  try
    fProcurar.btnImprimir.Visible := true;
    fProcurar.RadioGroup1.Visible := true;
    //DmCitrus.var_segmento := 3;
    //DmCitrus.var_regiao   := 1;
    scds_forn_proc.Params.ParamByName('pRegiao').AsInteger := 1;
    scds_forn_proc.Params.ParamByName('pSegmento').AsInteger := 3;
    fProcurar.BtnProcurar.Click;
    fProcurar.EvDBFind1.DataField := 'NOMEFORNECEDOR';
    fProcurar.RadioGroup2.Visible := True;
    fProcurar.ShowModal;
    DM.ID_FORNECEDOR := scds_forn_procCODFORNECEDOR.AsInteger;
    ds_contratoFOR_ID.AsInteger := scds_forn_procCODFORNECEDOR.AsInteger;
    ds_contratoPROPRIETARIO.AsString := scds_forn_procNOMEFORNECEDOR.AsString;
  finally
    scds_forn_proc.Close;
    fProcurar.Free;
  end;
end;

procedure Tf_Contratos.btn5Click(Sender: TObject);
begin
  //inherited;
  DM.ID_CONTRATO := ds_contratoCTR_ID.AsInteger;
  DM.ID_PROPRIEDADE := ds_contratoPRO_ID.AsInteger;
  f_VariedadeContrato := Tf_VariedadeContrato.Create(Application);
  try
    f_VariedadeContrato.ShowModal;
  finally
    f_VariedadeContrato.Free;
  end;
end;

procedure Tf_Contratos.btn6Click(Sender: TObject);
begin
  inherited;
  if (ds_contrato.State = dsInactive) then
     Exit;

  if (ds_contrato.State = dsBrowse) then
     ds_contrato.Edit;
     
  fProcurar:= TfProcurar.Create(self,buscaFrete);
  try
    fProcurar.BtnProcurar.Click;
    fProcurar.EvDBFind1.DataField := 'FAIXA';
    fProcurar.ShowModal;
    if (ds_contrato.State = dsBrowse) then
      ds_contrato.Edit;    
    ds_contratoVALOR_FRETE.Value := buscaFreteVALOR_FAIXA.Value;
  finally
    buscaFrete.Close;
    fProcurar.Free;
  end;
end;

procedure Tf_Contratos.btn7Click(Sender: TObject);
begin
  inherited;
  if (ds_contrato.State = dsInactive) then
     Exit;

  if (ds_contrato.State = dsBrowse) then
     ds_contrato.Edit;
     
  fProcurar:= TfProcurar.Create(self,buscaSafra);
  try
    fProcurar.BtnProcurar.Click;
    fProcurar.EvDBFind1.DataField := 'DESCSAFRA';
    fProcurar.ShowModal;
    if (ds_contrato.State = dsBrowse) then
      ds_contrato.Edit;
    ds_contratoSAFRA.AsString := buscaSafraDESCSAFRA.AsString;
  finally
    buscaSafra.Close;
    fProcurar.Free;
  end;
end;

procedure Tf_Contratos.btnIncluirClick(Sender: TObject);
begin
  if (ds_contrato.Active) then
      ds_contrato.Close;
  ds_contrato.Params[0].Clear;    
  inherited;
  btn4.Click;
  ds_contratoVALOR_FRETE.AsFloat := 0;
  ds_contratoVALOR_COLHEITA.AsFloat := 0;
  ds_contratoVALOR_COMICAO.AsFloat := 0;
  btn7.Click;
  btn3.Click;  
  btn1.Click;
end;

end.
